import { createStore } from 'vuex'
import createPersistedState from 'vuex-persistedstate'
import modules from './modules'
 
// Check if running in Electron environment 
const isElectron = window && window.process  && window.process.type  
 
// Electron-compatible storage 
const electronStorage = isElectron ? {
  getItem: (key) => window.ipcRenderer.sendSync('vuex-get',  key),
  setItem: (key, value) => window.ipcRenderer.send('vuex-set',  key, value),
  removeItem: (key) => window.ipcRenderer.send('vuex-remove',  key)
} : {
  getItem: (key) => localStorage.getItem(key), 
  setItem: (key, value) => localStorage.setItem(key,  value),
  removeItem: (key) => localStorage.removeItem(key) 
}
 
export default createStore({
  modules,
  plugins: [
    createPersistedState({
      storage: electronStorage,
      paths: ['user', 'settings']
    })
  ],
  strict: process.env.NODE_ENV  !== 'production'
})